package com.cskaoyan.controller;

import com.cskaoyan.bean.AdminInfoBean;
import com.cskaoyan.bean.InfoData;
import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.PO.MarketAdmin;
import com.cskaoyan.bean.PO.MarketLog;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.service.AdminAuthService;
import com.cskaoyan.service.AdminUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * 管理员登入注册
 * @author wenkaikai
 * @since 2022/08/12 16:50
 */
@RestController
@RequestMapping("admin/auth")
public class AdminAuthController {
    @Autowired
    AdminAuthService adminAuthService;



    @PostMapping("login")
    public BaseRespVo<LoginUserData> loginNLog(@RequestBody Map map, HttpServletRequest request) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");


        Subject subject = SecurityUtils.getSubject();
        MarketToken authenticationToken = new MarketToken(username, password, "admin");
        try {
            subject.login(authenticationToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        Session session = subject.getSession();

        // log记录
        session.setAttribute("logAdmin",username);
        session.setAttribute("logType", 1);
        session.setAttribute("logAction", "登录");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult","");
        session.setAttribute("logComment","");

        boolean authenticated = subject.isAuthenticated();
        if (!authenticated) {
            session.setAttribute("logStatus",false);
            session.setAttribute("logResult", "用户帐号或密码不正确");
            return BaseRespVo.invalidParameter("登录失败");
        }
        PrincipalCollection principals = subject.getPrincipals();
        MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();

        MarketAdmin admin = adminAuthService.getAdminInfo(primaryPrincipal);
        String remoteAddr = request.getRemoteAddr();
        primaryPrincipal.setLastLoginIp(remoteAddr);
        adminAuthService.updateData(primaryPrincipal);

        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar(admin.getAvatar());
        adminInfo.setNickName(primaryPrincipal.getUsername());
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken((String) session.getId());
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("info")
    public BaseRespVo info(String token) {

        Subject subject = SecurityUtils.getSubject();
        Serializable subjectToken = subject.getSession().getId();
        MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();


        InfoData infoData = new InfoData();
        infoData.setName(primaryPrincipal.getUsername());

        MarketAdmin admin = adminAuthService.getAdminInfo(primaryPrincipal);
        infoData.setAvatar(admin.getAvatar());

        ArrayList<String> roles = adminAuthService.getroles(admin);
        infoData.setRoles(roles);

        ArrayList<String> perms = adminAuthService.getPerms(admin);
        infoData.setPerms(perms);

        return BaseRespVo.ok(infoData);

//        InfoData infoData = new InfoData();
//        infoData.setName("admin123");
//        infoData.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        ArrayList<String> roles = new ArrayList<>();
//        roles.add("超级管理员");
//        infoData.setRoles(roles);
//        ArrayList<String> perms = new ArrayList<>();
//        perms.add("*");
//        infoData.setPerms(perms);
//        return BaseRespVo.ok(infoData);

    }

    @RequestMapping("logout")
    public BaseRespVo logoutNLog() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("logType", 1);
        session.setAttribute("logAction", "退出");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult","");
        session.setAttribute("logComment","");

//        subject.logout();
        return BaseRespVo.ok("成功");
    }
}